//
//  MainViewCtr.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/4/13.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit

class MainViewCtr: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

//       tabbar_home
//        tabbar_home_highlighted
        tabBar.tintColor = UIColor.orange

        addChildViewControllers();
        // Do any additional setup after loading the view.
    }
    
    //添加所有子控制器
    func addChildViewControllers(){
        /*
        addChild(HomeViewCtr(), title: "微博", imageName: "tabbar_home", higlightImage: "tabbar_home_highlighted")
        addChild(VideoViewCtr(), title: "视频", imageName: "tabbar_compose_icon_add", higlightImage: "tabbar_compose_icon_add_highlighted")
        addChild(DiscoverViewCtr(), title: "发现", imageName: "tabbar_discover", higlightImage: "tabbar_discover_highlighted")
        addChild(MessageViewCtr(), title: "消息", imageName: "tabbar_message_center", higlightImage: "tabbar_message_center_highlighted")
        addChild(MineViewCtr(), title: "我", imageName: "tabbar_profile", higlightImage: "tabbar_profile_highlighted")
         */
        
        addChild("HomeViewCtr", title: "微博", imageName: "tabbar_home", higlightImage: "tabbar_home_highlighted")
        addChild("VideoViewCtr", title: "视频", imageName: "tabbar_compose_icon_add", higlightImage: "tabbar_compose_icon_add_highlighted")
        addChild("DiscoverViewCtr", title: "发现", imageName: "tabbar_discover", higlightImage: "tabbar_discover_highlighted")
        addChild("MessageViewCtr", title: "消息", imageName: "tabbar_message_center", higlightImage: "tabbar_message_center_highlighted")
        addChild("MineViewCtr", title: "我", imageName: "tabbar_profile", higlightImage: "tabbar_profile_highlighted")
    }
    
    //添加子控制器
//    func addChild(_ childController: UIViewController, title: String, imageName: String, higlightImage: String) {
    func addChild(_ childControllerName: String, title: String, imageName: String, higlightImage: String) {

        
//        if let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String{
//            let cls : AnyClass? = NSClassFromString(name + "." + childControllerName)
//            let typeCls = cls as? UIViewController.Type
//        }
        
        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            NSLog(message: "获取命名空间失败")
            return
        }
        
        let cls : AnyClass? = NSClassFromString(name + "." + childControllerName)
        
        guard let typeCls = cls as? UIViewController.Type else {
            NSLog(message: "cls不能当作UIViewController")
            return
        }

        let childController = typeCls.init()
//        NSLog(message: childConrtoll)
        
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: higlightImage)
        let nav = UINavigationController(rootViewController: childController)
        addChild(nav)
    }
}
